package com.course.component.util;
/*
@author By 林中小鸟
@package  com.self.introduction.component.util
@create 2021 - 10 - 2021\10\11 0011
@QQ: 357182695
@Em: themooncss@gmail.com
*/

import org.apache.commons.lang3.time.DateFormatUtils;

import java.security.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {


    public static final String TAG = DateUtil.class.getSimpleName();

    /**
     * 日期类型 *
     */
    public static final String yyyyMMDD = "yyyy-MM-dd";
    public static final String yyyyMMddHHmmss2 = "yyyy-MM-dd_HH-mm-ss";
    public static final String yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String HHmmss = "HH:mm:ss";// 24小时制
    public static final String hhmmss = "hh:mm:ss";// 12小时制
    public static final String LOCALE_DATE_FORMAT = "yyyy年M月d日 HH:mm:ss";
    public static final String MMDD_CHAR = "M月d日";
    public static final String LOCALE_DATE_FORMAT2 = "yyyy年M月d日";
    public static final String DB_DATA_FORMAT = "yyyy-MM-DD HH:mm:ss";
    public static final String NEWS_ITEM_DATE_FORMAT = "hh:mm M月d日 yyyy";
    public static final String MMddHHmm = "MM月DD日 HH:mm";

    public static String PATTERN = "yyyy/MM/dd HH:mm";
    public static String NewPattern = "yyyy-MM-dd HH:mm";
    public static String YYYMMDD = "yyyy/MM/dd";
    public static String HHmm = "HH:mm";
    private static int age;
    private Date birthday;

    private DateUtil() {

    }


    public static String getStringTime(int dateType) {
        Calendar c = Calendar.getInstance(); // 当时的日期和时间
        int hour; // 需要更改的小时
        int day; // 需要更改的天数
        switch (dateType) {
            case 0: // 今天
                hour = c.get(Calendar.HOUR_OF_DAY);
                c.set(Calendar.HOUR_OF_DAY, hour);
                break;
            case 1: // 昨天
                day = c.get(Calendar.DAY_OF_MONTH) - 1;
                c.set(Calendar.DAY_OF_MONTH, day);
                break;
            case 2: // 前天
                day = c.get(Calendar.DAY_OF_MONTH) - 2;
                c.set(Calendar.DAY_OF_MONTH, day);
                break;
            case 3: // 最近七天
                day = c.get(Calendar.DAY_OF_MONTH) - 7;
                c.set(Calendar.DAY_OF_MONTH, day);
                break;
            case 4: // 最近15天
                day = c.get(Calendar.DAY_OF_MONTH) - 15;
                c.set(Calendar.DAY_OF_MONTH, day);
                break;
            case 5: // 最近30天
                day = c.get(Calendar.DAY_OF_MONTH) - 30;
                c.set(Calendar.DAY_OF_MONTH, day);
                break;
            case 6: // 最近90天
                day = c.get(Calendar.DAY_OF_MONTH) - 90;
                c.set(Calendar.DAY_OF_MONTH, day);
                break;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(c.getTimeInMillis());
        String res = simpleDateFormat.format(date);
        return res;
    }

    public static String dateToString(Date date, String pattern) throws Exception {
        return new SimpleDateFormat(pattern).format(date);
    }

    public static String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String getTime2(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    public static String getTime3(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 将Date类型转换为日期字符串
     *
     * @param date Date对象
     * @param type 需要的日期格式
     * @return 按照需求格式的日期字符串
     */
    public static String formatDate(Date date, String type) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(type);
            return df.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String formatDate(String date, String type) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(type);
            return df.format(parseDate(date, yyyyMMddHHmmss));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String formatDateTwo(String date, String type) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(type);
            return df.format(parseDate(date, YYYMMDD));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 得到日
     *
     * @param date Date对象
     * @return 日
     */
    public static int getDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 得到月
     *
     * @param date Date对象
     * @return 月
     */
    public static int getMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MONTH) + 1;
    }

    /**
     * 得到年
     *
     * @param date Date对象
     * @return 年
     */
    public static int getYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }

    /**
     * 得到小时
     *
     * @param date      Date对象
     * @param hourOfDay 是否是24小时制
     * @return 年
     */
    public static int getHour(Date date, boolean hourOfDay) {
        Calendar c = Calendar.getInstance();
        if (date != null) {
            c.setTime(date);
        }
        return c.get(hourOfDay ? Calendar.HOUR_OF_DAY : Calendar.HOUR);
    }

    /**
     * 得到分钟
     *
     * @param date Date对象
     * @return 年
     */
    public static int getMinute(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MINUTE);
    }

    /**
     * 将日期字符串转换为Date类型
     *
     * @param dateStr 日期字符串
     * @param type    日期字符串格式
     * @return Date对象
     */
    public static Date parseDate(String dateStr, String type) {
        SimpleDateFormat df = new SimpleDateFormat(type);
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将日期字符串转换为Calendar类型
     *
     * @param dateStr 日期字符串
     * @param type    日期字符串格式
     * @return Calendar对象
     */
    public static Calendar parseCalendar(String dateStr, String type) {
        if (dateStr.isEmpty()) {
            return Calendar.getInstance();
        }
        SimpleDateFormat df = new SimpleDateFormat(type);
        Calendar date = Calendar.getInstance();
        try {
            date.setTime(df.parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    public static String translateDate2(Date date) {
//        Date date = parseDate(dateTime, DateUtil.yyyyMMddHHmmss);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        switch (translateDate(date.getTime())) {
            case "今天":
                return "今天" + date.getHours() + ":" + date.getMinutes();
            case "昨天":
                return "昨天" + date.getHours() + ":" + date.getMinutes();
            case "前天":
                return "前天" + date.getHours() + ":" + date.getMinutes();
            default:
                return calendar.get(Calendar.MONTH) + 1 + "月" + calendar.get(Calendar
                        .DAY_OF_MONTH) + "日";
        }
    }

    /**
     * 转换日期 将日期转为今天, 昨天, 前天, XXXX-XX-XX, ...
     *
     * @param time 时间
     * @return 当前日期转换为更容易理解的方式
     */
    public static String translateDate(Long time) {
        long oneDay = 24 * 60 * 60 * 1000;
        Calendar current = Calendar.getInstance();
        Calendar today = Calendar.getInstance(); // 今天

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
        // Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        long todayStartTime = today.getTimeInMillis();

        if (time >= todayStartTime && time < todayStartTime + oneDay) { // today
            return "今天";
        } else if (time >= todayStartTime - oneDay && time < todayStartTime) { // yesterday
            return "昨天";
        } else if (time >= todayStartTime - oneDay * 2 && time < todayStartTime - oneDay) { // the
            // day
            // before
            // yesterday
            return "前天";
        } else if (time > todayStartTime + oneDay) { // future
            return "将来某一天";
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(time);
            return dateFormat.format(date);
        }
    }

    /**
     * 将分钟转换为*天*时*分
     */
    public static int[] formatMin(int min) {
        int[] ints = new int[3];
        int days = min / (60 * 24);
        int hours = min / 60 - days * 24;
        int minutes = min % 60;

        ints[0] = days;
        ints[1] = hours;
        ints[2] = minutes;
        return ints;
    }

    /**
     * 将分钟转换为*时
     *
     * @param min
     * @return
     */
    public static int formatToHour(int min) {
        return min <= 60 ? 1 : (int) (min / 60);
    }

    /**
     * 判断是不是闰年
     *
     * @param year
     * @return
     */
    public static boolean isLeapYear(int year) {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    /**
     * DatePopupCtrl 根据最大天数，来增加天数List
     *
     * @param year
     * @param month
     * @return
     */
    public static ArrayList<String> addDayLists(int year, int month) {
        int maxDay = 0;
        if (isLeapYear(year) && month == 2) {
            // 闰年
            maxDay = 29;
        } else {
            // 非闰年
            switch (month) {
                case 2:
                    maxDay = 28;
                    break;
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    maxDay = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    maxDay = 30;
                    break;
                default:
                    break;
            }
        }
        return getDayData(maxDay);
    }

    private static ArrayList<String> getDayData(int maxDay) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 1; i <= maxDay; i++) {
            list.add(String.valueOf(i));
        }
        return list;
    }

    /**
     * 获取当前月
     *
     * @return
     */
    public static int getNowMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }


    /**
     * 获取半年后的时间
     *
     * @return
     */
    public static Calendar getHalfYeaAfter() {
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.MONTH, instance.get(Calendar.MONTH) + 6);
        return instance;
    }

    /**
     * 获取当前年
     *
     * @return
     */
    public static int getNowYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 获取当前日
     *
     * @return
     */
    public static int getNowDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取月份List
     *
     * @return
     */
    public static ArrayList<String> getMonthData() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            list.add(String.valueOf(i));
        }
        return list;
    }

    /**
     * 获取小时List
     *
     * @return
     */
    public static ArrayList<String> getHourData() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            list.add(String.valueOf(i));
        }
        return list;
    }

    /**
     * 获取分钟List
     *
     * @return
     */
    public static ArrayList<String> getMinData() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 60; i += 10) {
            list.add(String.valueOf(i));
        }
        return list;
    }

    /**
     * 获取分钟List
     *
     * @return
     */
    public static ArrayList<String> getMinData2() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            list.add(String.valueOf(i));
        }
        return list;
    }

    /**
     * 获取当前小时
     *
     * @return
     */
    public static int getNowHour() {
        return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    }

    public static int getStartHour() {
        int nowMin = getNowMin();
        int nowHour = getNowHour();
        if (nowMin % 10 != 0) {
            nowMin = (nowMin / 10 + 1) * 10;
            if (nowMin > 59) {
                nowHour++;
            }
        }
        if (nowHour > 23) {
            nowHour -= 24;
        }
        return nowHour;
    }

    /**
     * 整10分钟显示
     *
     * @return
     */
    public static int getStartMin() {
        int nowMin = getNowMin();
        if (nowMin % 10 != 0) {
            nowMin = (nowMin / 10 + 1) * 10;
            if (nowMin > 59) {
                nowMin -= 60;
            }
        }
        return nowMin;
    }

    /**
     * 整15分钟显示
     *
     * @return
     */
    public static int getStartMinFifteen() {
        int nowMin = getNowMin();
        if (nowMin % 15 != 0) {
            nowMin = (nowMin / 15 + 1) * 15;
            if (nowMin > 59) {
                nowMin -= 60;
            }
        }
        return nowMin;
    }

    /**
     * 比当前时间多30分钟
     *
     * @return
     */
    public static int getEndHour() {
        int hour = getStartHour();
        if (getStartMin() >= 30) {
            hour++;
        }
        if (hour > 23) {
            hour -= 24;
        }
        return hour;
    }

    public static int getEndMin() {
        int min = getStartMin() + 30;
        if (min > 59) {
            min -= 60;
        }
        return min;
    }

    /**
     * 获取当前分钟
     *
     * @return
     */
    public static int getNowMin() {
        return Calendar.getInstance().get(Calendar.MINUTE);
    }

    /**
     * Timestamp格式化
     *
     * @param timestamp
     * @param pattern
     * @return
     */
    public static String formatTimestamp(Timestamp timestamp, String pattern) {
        return new SimpleDateFormat(pattern).format(timestamp);
    }

    /**
     * 根据分钟显示天 时 分 2015-12-16zds
     *
     * @return
     */
    public static String formatTime(int min) {
        int[] formatMin = DateUtil.formatMin(min);
        return (formatMin[0] > 0 ? formatMin[0] + "天" : "") + (formatMin[1] > 0 ? formatMin[1] +
                "时" : "") + (formatMin[2] > 0 ? formatMin[2] + "分钟" : "");
    }


    public static String formatTimeSecs(int s) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (s <= 0) {
            return "0秒";
        } else if (s < 60) {
            timeStr = s + "秒";
        } else {
            minute = s / 60;
            if (minute < 60) {
                second = s % 60;
                timeStr = minute + "分" + second + "秒";
            } else {
                hour = minute / 60;
                if (hour > 99) {
                    return "99:59:59";
                }
                minute = minute % 60;
                second = s - hour * 3600 - minute * 60;
                timeStr = hour + "小时" + minute + "分" + second + "秒";
            }
        }
        return timeStr;
    }

    /**
     * 时间格式 "yyyy/MM/dd HH:mm" 比较结束时间是否大于开始时间
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return true 大于； false 不大于
     */
    public static boolean timeComparison(String startTime, String endTime) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(PATTERN);
            Date startDate = format.parse(startTime);
            Date endDate = format.parse(endTime);
            if (endDate.getTime() > startDate.getTime()) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
     * @author fy.zhang
     */
    public static String formatDurings(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
    }

    /**
     * 租车的天数
     *
     * @param mss
     * @return
     */
    public static double formatDuring(long mss) {
        double days = mss / (1000 * 60 * 60 * 24.0);
        double decimalPart = days - ((int) days);
        if (decimalPart == 0) {
            return days;
        } else if (decimalPart <= 0.5) {
            return ((int) days) + 0.5;
        } else {
            return ((int) days) + 1;
        }
    }

    /**
     * @param begin 时间段的开始
     * @param end   时间段的结束
     * @return 输入的两个Date类型数据之间的时间间格用* days * hours * minutes * seconds的格式展示
     * @author fy.zhang
     */
    public static double formatDuring(Date begin, Date end) {
        if (null == begin || null == end) {
            return 0;
        }
        return formatDuring(end.getTime() - begin.getTime());
    }

    /**
     * 向前推30分钟
     *
     * @param date
     * @return
     */
    public static String formatDateAddMin(Date date) {
        date.setTime(date.getTime() + 35 * 60 * 1000);
        String formatDate = DateUtil.formatDate(date, PATTERN);
        return formatDate.substring(0, formatDate.length() - 1) + "0";
    }

    /**
     * 向前推一天
     *
     * @param date
     * @return
     */
    public static String formatDateAddDay(Date date) {
        date.setTime(date.getTime() + 24 * 60 * 60 * 1000 + 35 * 60 * 1000);
        String formatDate = DateUtil.formatDate(date, PATTERN);
        return formatDate.substring(0, formatDate.length() - 1) + "0";
    }

    /**
     * 代驾默认时间
     *
     * @param date
     * @return
     */
    public static String formatDateDrive(Date date) {
        date.setTime(date.getTime() + 5 * 60 * 1000);
        String formatDate = DateUtil.formatDate(date, PATTERN);
        date.setTime(date.getTime() + 30 * 60 * 1000);
        String time = DateUtil.formatDate(date, HHmm);
        return formatDate.substring(0, formatDate.length() - 1) + "0" + "-" + time.substring(0,
                time.length() - 1) + "0";
    }

    /**
     * 日期
     *
     * @param date
     * @return
     */
    public static String formatDatePool(Date date) {
        return DateUtil.formatDate(date, "yyyy-MM-dd");
    }

    /**
     * 时间HHMM时间格式
     *
     * @param date
     * @return
     */
    public static String formatDatePoolTime(Date date) {
        date.setTime(date.getTime() + 5 * 60 * 1000);
        String timeStart = DateUtil.formatDate(date, HHmm);
        date.setTime(date.getTime() + 30 * 60 * 1000);
        String timeEnd = DateUtil.formatDate(date, HHmm);
        return timeStart.substring(0, timeStart.length() - 1) + "0" + "-" + timeEnd.substring(0,
                timeEnd.length() - 1) + "0";
    }

    public static int getDays(int month) {
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.YEAR, getNowYear());
        instance.set(Calendar.MONTH, month - 1);
        return instance.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 转换日期 转换为 昨天 前天   HH:mm
     *
     * @param time long类型 时间
     * @return
     */
    public static String transformDate(long time) {
        long oneDay = 24 * 60 * 60 * 1000;
        Calendar current = Calendar.getInstance();
        Calendar today = Calendar.getInstance(); // 今天

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
        // Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        long todayStartTime = today.getTimeInMillis();

        if (time >= todayStartTime && time < todayStartTime + oneDay) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
            Date date = new Date(time);
            return dateFormat.format(date);
        } else if (time >= todayStartTime - oneDay && time < todayStartTime) {
            return "昨天";
        } else if (time >= todayStartTime - oneDay * 2 && time < todayStartTime - oneDay) {
            return "前天";
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日");
            Date date = new Date(time);
            return dateFormat.format(date);
        }
    }

    /**
     * 转换日期 转换为 昨天 前天   HH:mm
     *
     * @param time long类型 时间
     * @return
     */
    public static String transformDateNew(long time) {
        long oneDay = 24 * 60 * 60 * 1000;
        Calendar current = Calendar.getInstance();
        Calendar today = Calendar.getInstance(); // 今天

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
        // Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        long todayStartTime = today.getTimeInMillis();

        if (time >= todayStartTime && time < todayStartTime + oneDay) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
            Date date = new Date(time);
            return "今天 " + dateFormat.format(date);
        } else if (time >= todayStartTime - oneDay && time < todayStartTime) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
            Date date = new Date(time);
            return "昨天 " + dateFormat.format(date);
        } else if (time >= todayStartTime - oneDay * 2 && time < todayStartTime - oneDay) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
            Date date = new Date(time);
            return "前天 " + dateFormat.format(date);
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
            Date date = new Date(time);
            return dateFormat.format(date);
        }
    }

    /**
     * 格式化时间
     *
     * @param time
     * @return
     */
    public static String parseData(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(yyyyMMddHHmmss);
        Date date = new Date(time);
        return dateFormat.format(date);
    }

    /**
     * 转换日期 转换为 昨天 前天   HH:mm
     *
     * @param date String 类型 时间
     *             默认类型 yyyyMMddHHmmss
     * @return
     */
    public static String transformDate(String date) {
        return transformDate(parseDate(date, yyyyMMddHHmmss).getTime());
    }

    public static String transformDateNew(String date) {
        return transformDateNew(parseDate(date, yyyyMMddHHmmss).getTime());
    }

    /**
     * 转换日期 转换为 昨天 前天   HH:mm
     *
     * @param date    String 类型 时间
     * @param pattern 格式化时间
     * @return
     */
    public static String transformDate(String date, String pattern) {
        return transformDate(parseDate(date, pattern).getTime());
    }

    /**
     * 计算与当前日期之间相差的天数
     *
     * @param bdate 的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(String bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmss);
        Calendar cal = Calendar.getInstance(); // 今天
        cal.setTimeInMillis(System.currentTimeMillis());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        long time1 = cal.getTimeInMillis();
        try {
            cal.setTime(sdf.parse(bdate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time2 = cal.getTimeInMillis();
        long between_days = 0;
        if (time2 > time1) {
            between_days = (time2 - time1) / (1000 * 3600 * 24);
        } else {
            between_days = (time1 - time2) / (1000 * 3600 * 24) + 1;
        }


        return Integer.parseInt(String.valueOf((int) (between_days)));
    }

    public static double daysBetween(String start, String end) {
        long between_days = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(NewPattern);
            Calendar cal = Calendar.getInstance();// 今天
            cal.setTimeInMillis(sdf.parse(start).getTime());
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(end));
            long time2 = cal.getTimeInMillis();

            if (time2 > time1) {
                between_days = time2 - time1;
            } else {
                between_days = time1 - time2;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return formatDuring(between_days);
    }

    /**
     * 计算时间相差多少年多少月多少天
     *
     * @param endDateStr
     * @return
     */
    public static String remainDateToString(String endDateStr) {
        if (endDateStr.isEmpty()) {
            return "2天";
        }
        Calendar calS = Calendar.getInstance();
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = new SimpleDateFormat(yyyyMMDD).parse(formatDate(new Date(), yyyyMMDD));
            endDate = new SimpleDateFormat(yyyyMMDD).parse(endDateStr);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        calS.setTime(startDate);
        int startY = calS.get(Calendar.YEAR);
        int startM = calS.get(Calendar.MONTH);
        int startD = calS.get(Calendar.DATE);
        int startDayOfMonth = calS.getActualMaximum(Calendar.DAY_OF_MONTH);

        calS.setTime(endDate);
        int endY = calS.get(Calendar.YEAR);
        int endM = calS.get(Calendar.MONTH);
        // 处理2011-01-10到2011-01-10，认为服务为一天
        int endD = calS.get(Calendar.DATE) + 1;
        int endDayOfMonth = calS.getActualMaximum(Calendar.DAY_OF_MONTH);

        StringBuilder sBuilder = new StringBuilder();
        if (endDate.compareTo(startDate) < 0) {
            return sBuilder.append("过期").toString();
        }
        int lday = endD - startD;
        if (lday < 0) {
            endM = endM - 1;
            lday = startDayOfMonth + lday;
        }
        // 处理天数问题，如：2011-01-01 到 2013-12-31 2年11个月31天 实际上就是3年
        if (lday == endDayOfMonth) {
            endM = endM + 1;
            lday = 0;
        }
        int mos = (endY - startY) * 12 + (endM - startM);
        int lyear = mos / 12;
        int lmonth = mos % 12;
        if (lyear > 0) {
            sBuilder.append(lyear + "年");
        }
        if (lmonth > 0) {
            sBuilder.append(lmonth + "个月");
        }

        // 假如是小于一岁的时候，
        if (lyear == 0) {
            // sBuilder.append(lday+"天");
            if (lday - 1 > 0) {
                sBuilder.append(lday - 1 + "天");
            }
        } else {
            if (lday > 0) {
                sBuilder.append(lday + "天");
            }
        }
        return sBuilder.toString();
    }


    /**
     * 根据用户生日计算年龄
     */
    public static int getAgeByBirthday(String calendar) {
        Calendar birthday = parseCalendar(calendar, yyyyMMDD);
        Calendar cal = Calendar.getInstance();

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);


        int yearBirth = birthday.get(Calendar.YEAR);
        int monthBirth = birthday.get(Calendar.MONTH) + 1;
        int dayOfMonthBirth = birthday.get(Calendar.DAY_OF_MONTH);

        Integer age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                age--;
            }
        }
        return age;
    }

    public static String getCurrentTime() {

        Date date = new Date();

        String time = date.toString();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String sim = dateFormat.format(date);


        return sim;
    }

    /**
     * 转换日期 转换为更为人性化的时间
     *
     * @param time 时间
     * @return
     */
    @SuppressWarnings("unused")
    private String translateDate(long time, long curTime) {
        long oneDay = 24 * 60 * 60;
        Calendar today = Calendar.getInstance(); // 今天
        today.setTimeInMillis(curTime * 1000);
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        long todayStartTime = today.getTimeInMillis() / 1000;
        if (time >= todayStartTime) {
            long d = curTime - time;
            if (d <= 60) {
                return "1分钟前";
            } else if (d <= 60 * 60) {
                long m = d / 60;
                if (m <= 0) {
                    m = 1;
                }
                return m + "分钟前";
            } else {
                SimpleDateFormat dateFormat = new SimpleDateFormat("今天 HH:mm");
                Date date = new Date(time * 1000);
                String dateStr = dateFormat.format(date);
                if (!dateStr.isEmpty() && dateStr.contains(" 0")) {
                    dateStr = dateStr.replace(" 0", " ");
                }
                return dateStr;
            }
        } else {
            if (time < todayStartTime && time > todayStartTime - oneDay) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("昨天 HH:mm");
                Date date = new Date(time * 1000);
                String dateStr = dateFormat.format(date);
                if (!dateStr.isEmpty() && dateStr.contains(" 0")) {

                    dateStr = dateStr.replace(" 0", " ");
                }
                return dateStr;
            } else if (time < todayStartTime - oneDay && time > todayStartTime - 2 * oneDay) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("前天 HH:mm");
                Date date = new Date(time * 1000);
                String dateStr = dateFormat.format(date);
                if (!dateStr.isEmpty() && dateStr.contains(" 0")) {
                    dateStr = dateStr.replace(" 0", " ");
                }
                return dateStr;
            } else {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date date = new Date(time * 1000);
                String dateStr = dateFormat.format(date);
                if (!dateStr.isEmpty() && dateStr.contains(" 0")) {
                    dateStr = dateStr.replace(" 0", " ");
                }
                return dateStr;
            }
        }
    }


    //根据当前日期，向后数三个月（若当前day不是1号，为满足至少90天，则需要向后数4个月）
    public List<String> getAfterDateList() {
        List<String> list = new ArrayList<String>();
        Calendar c = Calendar.getInstance();
        int i = c.get(Calendar.MONTH);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
        for (int value = 0; value < 4; value++) {
            int nowYear = c.get(Calendar.YEAR);
            c.set(Calendar.MONTH, i + value);
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            final int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            if (nowYear == year) {
                list.add(year + "-" + (month + 1) + "-" + lastDay + "");
            } else {
                list.add(year + "-" + (month + 1) + "-" + lastDay + "");
            }
        }
        return list;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToYD(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM-dd");
        Date date2 = new Date(ts * 1000L);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp2(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /**
     * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
     * @author fy.zhang
     */
    public static String formatDuringDay(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        return days + "";
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt * 1000L);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将毫秒转时分秒
     *
     * @param time
     * @return
     */
    public static String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        return hours > 0 ? String.format("%02d'%02d'%02d''", hours, minutes, seconds) : String.format("%02d'%02d''", minutes, seconds);
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToTime(String s) {
        String data;
        String time;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt * 1000L);
        data = simpleDateFormat.format(date);
        time = simpleDateFormat2.format(date);
        return data + " \n " + time;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampTime2(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    // 根据年月日计算年龄,birthTimeString:"1994-11-14"
    public static int getAgeFromBirthTime(String birthTimeString) {
        // 先截取到字符串中的年、月、日
        String strs[] = birthTimeString.trim().split("-");
        int selectYear = Integer.parseInt(strs[0]);
        int selectMonth = Integer.parseInt(strs[1]);
        int selectDay = Integer.parseInt(strs[2]);
        // 得到当前时间的年、月、日
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayNow = cal.get(Calendar.DATE);

        // 用当前年月日减去生日年月日
        int yearMinus = yearNow - selectYear;
        int monthMinus = monthNow - selectMonth;
        int dayMinus = dayNow - selectDay;

        int age = yearMinus;// 先大致赋值
        if (yearMinus < 0) {// 选了未来的年份
            age = 0;
        } else if (yearMinus == 0) {// 同年的，要么为1，要么为0
            if (monthMinus < 0) {// 选了未来的月份
                age = 0;
            } else if (monthMinus == 0) {// 同月份的
                if (dayMinus < 0) {// 选了未来的日期
                    age = 0;
                } else if (dayMinus >= 0) {
                    age = 1;
                }
            } else if (monthMinus > 0) {
                age = 1;
            }
        } else if (yearMinus > 0) {
            if (monthMinus < 0) {// 当前月>生日月
            } else if (monthMinus == 0) {// 同月份的，再根据日期计算年龄
                if (dayMinus < 0) {
                } else if (dayMinus >= 0) {
                    age = age + 1;
                }
            } else if (monthMinus > 0) {
                age = age + 1;
            }
        }
        return age;
    }

    // 根据时间戳计算年龄
    public static int getAgeFromBirthTime(long birthTimeLong) {
        Date date = new Date(birthTimeLong * 1000l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String birthTimeString = format.format(date);
        return getAgeFromBirthTime(birthTimeString);
    }

    /**
     * 根据时间戳获取年龄
     *
     * @return
     */
    public int getAge(String birthday) {
            if (birthday != null) {
                String now = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                String bTime = DateFormatUtils.format(Long.parseLong(birthday), "yyyy-MM-dd HH:mm:ss");
                age = Integer.valueOf(now.substring(0, 4)) - Integer.valueOf(bTime.substring(0,4));
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String d = format.format(this.birthday);
                Date date = null;
                try {
                    date = format.parse(d);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Long nowYearDays = org.apache.commons.lang3.time.DateUtils.getFragmentInDays
     (new Date(), Calendar.YEAR);
                Long bYearDays = org.apache.commons.lang3.time.DateUtils.getFragmentInDays
     (date, Calendar.YEAR);
                if (bYearDays > nowYearDays) {
                    age--;
                }
            }
            return age;
        }

    //由出生日期获得年龄
    public static int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                age--;
            }
        }
        return age;
    }

    /**
     * 将出生日期与当前日期相减，获得年龄
     *
     * @param birthdayDate
     * @return
     */
    public static int getYears(Date birthdayDate) {
        String formatCurrent = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        Integer firstCu = formatCurrent.indexOf("-");
        Integer lastCu = formatCurrent.lastIndexOf("-");
        String currentYearStr = formatCurrent.substring(0, firstCu);
        String currentMonthStr = formatCurrent.substring(firstCu + 1, lastCu);
        String currentDayStr = formatCurrent.substring(lastCu + 1);
        int currentYear = Integer.valueOf(currentYearStr);
        int currentMonth = Integer.valueOf(currentMonthStr);
        int currentDay = Integer.valueOf(currentDayStr);

        String formatBirthday = new SimpleDateFormat("yyyy-MM-dd").format(birthdayDate);

        int first = formatBirthday.indexOf("-");
        int last = formatBirthday.lastIndexOf("-");
        String birthYearStr = formatBirthday.substring(0, first);
        String birthMonthStr = formatBirthday.substring(first + 1, last);
        String birthDayStr = formatBirthday.substring(last + 1);

        int birthYear = Integer.valueOf(birthYearStr);
        int birthMonth = Integer.valueOf(birthMonthStr);
        int birthDay = Integer.valueOf(birthDayStr);

        if (currentMonth > birthMonth) {
            return currentYear - birthYear;
        } else if (currentMonth == birthMonth) {
            if (currentDay >= birthDay) {
                return currentYear - birthYear;
            } else {
                return currentYear - birthYear - 1;
            }
        } else {
            return currentYear - birthYear - 1;
        }
    }

    /**
     * 格式化时间
     *
     * @param time
     * @return
     */
    public static String getFormatDate(long time) {
        String ftime = "";
        try {
            Date date = new Date(time);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ftime = format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ftime;
    }

    //判断是否超过24小时
    public static boolean getGreaterDays(String date1, String date2) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(date1);
        long lt2 = new Long(date2);
        Date start = new Date(lt);
        Date end = new Date(lt2);
        long cha = end.getTime() - start.getTime();
        //        double result = cha * 1.0 / (1000 * 60 * 60);
        double result = 24 * 1000 * 60 * 60;
        if (cha < result) {
            //System.out.println("可用");
            return true;
        } else {
            //System.out.println("已过期");
            return false;
        }
    }

    //判断是否超过14天
    public static boolean getGreaterHebdomad(String date1, String date2) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(date1);
        long lt2 = new Long(date2);
        Date start = new Date(lt);
        Date end = new Date(lt2);
        long cha = end.getTime() - start.getTime();
        //        long result = cha * 1.0 / (1000 * 60 * 60);
        long result = 14 * 24 * 1000 * 60 * 60;
        if (cha > result) {
            //System.out.println("可用");
            return true;
        } else {
            //System.out.println("已过期");
            return false;
        }
    }

    /**
     * 将服务器返回的毫秒 转换为天时分秒格式
     */
    public static String formatDateToMinute(long millis) {
        if (millis <= 0) {
            return "00:00:00";
        }
        long hours = 60 * 60 * 1000;
        long days = hours * 24; // 一天的毫秒

        long day = millis / days;
        long hour = (millis % days) / hours; // 小时
        long minute = (millis % hours) / (60 * 1000);
        long second = (millis % (1000 * 60)) / 1000;
        StringBuffer sBuffer = new StringBuffer(9);
        if (day > 0) {
            sBuffer.append(day + "天");
        }
        if (hour > 0) {
            sBuffer.append(format(hour) + "时");
        }
        if (minute > 0) {
            sBuffer.append(format(minute) + "分");
        }
        if (second > 0) {
            sBuffer.append(format(second) + "秒");
        }
        return sBuffer.toString();
    }

    private static String format(long time) {
        if (time < 0) {
            return "00";
        } else if (time <= 9) {
            return "0" + time;
        } else {
            return Long.toString(time);
        }
    }
}
